# Copyright 2022 Flant JSC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

{!{- $ctx := . -}!}
{!{- $jobNames := dict }!}

name: Build and test for release branches

# On every push to release branches.
on:
  push:
    branches:
      - 'release-*'

env:
{!{ tmpl.Exec "werf_envs" | strings.Indent 2 }!}
{!{ tmpl.Exec "git_source_envs" | strings.Indent 2 }!}

# Cancel in-progress jobs for the same branch.
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
{!{ tmpl.Exec "git_info_job" $ctx | strings.Indent 2 }!}

  go_generate:
    name: Go Generate
    needs:
      - git_info
{!{ tmpl.Exec "go_generate_template" $ctx | strings.Indent 4 }!}

  workflow_render:
    name: Render workflow
    needs:
      - git_info
{!{ tmpl.Exec "workflow_render_template" $ctx | strings.Indent 4 }!}

  build_deckhouse:
    name: Build Deckhouse FE
    needs:
      - git_info
      - go_generate
      - workflow_render
    env:
      WERF_ENV: "FE"
{!{ tmpl.Exec "build_template" (slice $ctx "pre-release") | strings.Indent 4 }!}

  doc_web_build:
    name: Doc web build
    # Wait for success build of modules.
    needs:
      - git_info
{!{ tmpl.Exec "web_build_template" (slice $ctx "doc" "release") | strings.Indent 4 }!}

  main_web_build:
    name: Main web build
    # Wait for success build of modules.
    needs:
      - git_info
{!{ tmpl.Exec "web_build_template" (slice $ctx "main" "dev") | strings.Indent 4 }!}

  tests:
    name: Tests
    needs:
      - git_info
{!{ tmpl.Exec "tests_before_build_template" (slice $ctx "unit") | strings.Indent 4 }!}

  matrix_tests:
    name: Matrix tests
    needs:
      - git_info
{!{ tmpl.Exec "tests_before_build_template" (slice $ctx "matrix") | strings.Indent 4 }!}

  dhctl_tests:
    name: Dhctl Tests
    needs:
      - git_info
      - build_deckhouse
{!{ tmpl.Exec "tests_template" (slice $ctx "dhctl" "build_deckhouse") | strings.Indent 4 }!}

  golangci_lint:
    name: GolangCI Lint
    needs:
      - git_info
      - build_deckhouse
{!{ tmpl.Exec "tests_template" (slice $ctx "golangci_lint" "build_deckhouse") | strings.Indent 4 }!}

  openapi_test_cases:
    name: OpenAPI Test Cases
    needs:
      - git_info
      - build_deckhouse
{!{ tmpl.Exec "tests_template" (slice $ctx "openapi_test_cases" "build_deckhouse") | strings.Indent 4 }!}

  web_links_test:
    name: Web links test
    if: ${{ false || github.repository == 'deckhouse/deckhouse' }}
    needs:
      - git_info
      - doc_web_build
      - main_web_build
    continue-on-error: true
{!{ tmpl.Exec "web_links_test_template" (slice $ctx "release") | strings.Indent 4 }!}

  validators:
    name: Validators
    needs:
      - git_info
      - build_deckhouse
{!{ tmpl.Exec "tests_template" (slice $ctx "validators" "build_deckhouse") | strings.Indent 4 }!}

{!{/* Autodeploy documentation to production envs on push to release branch */}!}
{!{- $jobNames = coll.Merge $jobNames (dict "deploy_release_doc_prod_sel" "(Prod) Deploy release documentation") }!}
  deploy_release_doc_prod_sel:
    name: {!{ $jobNames.deploy_release_doc_prod_sel }!}
    needs:
      - git_info
      - doc_web_build
    if: ${{ startsWith(needs.git_info.outputs.ci_commit_ref_name, 'release-') && github.repository == 'deckhouse/deckhouse' }}
    runs-on: [self-hosted, regular]
    steps:
{!{ tmpl.Exec "started_at_output" . | strings.Indent 6 }!}
{!{ tmpl.Exec "checkout_full_step" $ctx | strings.Indent 6}!}
{!{ tmpl.Exec "login_readonly_registry_step" $ctx | strings.Indent 6 }!}
{!{ tmpl.Exec "login_dev_registry_step" $ctx | strings.Indent 6 }!}
{!{ tmpl.Exec "doc_release_version_template" | strings.Indent 6 }!}
{!{ tmpl.Exec "deploy_doc_template" "preproduction" | strings.Indent 6 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.deploy_release_doc_prod_sel) | strings.Indent 6 }!}

{!{/* Autodeploy documentation to stage envs on push to release branch */}!}
{!{- $jobNames = coll.Merge $jobNames (dict "deploy_release_doc_stage" "(Stage) Deploy release documentation") }!}
  deploy_release_doc_stage:
    name: {!{ $jobNames.deploy_release_doc_stage }!}
    needs:
      - git_info
      - doc_web_build
    if: ${{ startsWith(needs.git_info.outputs.ci_commit_ref_name, 'release-') && github.repository == 'deckhouse/deckhouse' }}
    runs-on: [self-hosted, regular]
    steps:
{!{ tmpl.Exec "started_at_output" . | strings.Indent 6 }!}
{!{ tmpl.Exec "checkout_full_step" $ctx | strings.Indent 6}!}
{!{ tmpl.Exec "login_readonly_registry_step" $ctx | strings.Indent 6 }!}
{!{ tmpl.Exec "login_dev_registry_step" $ctx | strings.Indent 6 }!}
{!{ tmpl.Exec "doc_release_version_template" | strings.Indent 6 }!}
{!{ tmpl.Exec "deploy_doc_template" "stage" | strings.Indent 6 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.deploy_release_doc_stage) | strings.Indent 6 }!}
